package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.exception.CustomException;
import com.tcoiss.common.core.utils.DateUtils;
import com.tcoiss.common.component.api.RemoteApiService;
import com.tcoiss.common.component.api.model.ApiParam;
import com.tcoiss.datafactory.domain.MqTopic;
import com.tcoiss.datafactory.mapper.MqTopicMapper;
import com.tcoiss.datafactory.service.IMqTopicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息主题发布Service业务层处理
 *
 * @author zw
 * @date 2021-05-13
 */
@Service
public class MqTopicServiceImpl extends ServiceImpl<MqTopicMapper, MqTopic> implements IMqTopicService {

    @Resource
    private RemoteApiService remoteApiService;
    @Resource
    private IMqTopicService iMqTopicService;

    @Override
    public List<MqTopic> queryList(MqTopic mqTopic) {
        LambdaQueryWrapper<MqTopic> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(mqTopic.getTopicCode())){
            lqw.eq(MqTopic::getTopicCode ,mqTopic.getTopicCode());
        }
        if (StringUtils.isNotBlank(mqTopic.getTopicName())){
            lqw.like(MqTopic::getTopicName ,mqTopic.getTopicName());
        }
        if (mqTopic.getModifyTime() != null){
            lqw.eq(MqTopic::getModifyTime ,mqTopic.getModifyTime());
        }
        if (mqTopic.getDataLevel() != null){
            lqw.eq(MqTopic::getDataLevel ,mqTopic.getDataLevel());
        }
        return this.list(lqw);
    }

    @Override
    public boolean syncKdTopic() {
        ApiParam apiParam = new ApiParam();
        apiParam.setApiCode("syncPublishTopic");
        String select = "number,name,enable,createtime,modifytime";
        Map<String, Object> param = new HashMap<>();
        param.put("select", select);
        apiParam.setParam(param);
        R<Map<String, Object>> r = remoteApiService.executeKdApi(apiParam);
        List<List<Object>> rows;
        Map<String, Object> map = r.getData();
        List<MqTopic> mtList = new ArrayList<>();
        if (r.getCode() == 200) {
            if (map.get("success").equals("true")) {
                Map<String, Object> data = (Map<String, Object>) map.get("data");
                rows = (List) data.get("rows");
                int count = Integer.valueOf(data.get("count").toString());
                if (count == 0) {
                    throw new CustomException("未查询到可同步数据", 5000);
                }
                if (count > 0) {
                    for (List<Object> row : rows) {
                        MqTopic mt = new MqTopic();
                        mt.setTopicCode(row.get(0).toString());
                        mt.setTopicName(row.get(1).toString());
                        mt.setDataLevel(Integer.parseInt(row.get(2).toString()));
                        mt.setCreateTime(DateUtils.strParseToDate(row.get(3)));
                        mt.setModifyTime(DateUtils.strParseToDate(row.get(4)));
                        mtList.add(mt);
                    }
                    iMqTopicService.remove(new QueryWrapper<>());
                    return iMqTopicService.saveBatch(mtList);
                }
            } else {
                throw new CustomException("同步消息主题调用API接口异常" + r.getMsg(),r.getCode());
            }
        }
        throw new CustomException(r.getMsg(), r.getCode());
    }

    @Override
    public boolean updateUseStatus(List<Long> asList, Integer status) {
        if (asList != null & status != null) {
            for (Long id : asList) {
                MqTopic ew = getById(id);
                if (ew.getDataLevel() != status) {
                    ew.setDataLevel(status);
                    this.updateById(ew);
                }
            }
            return true;
        }
        return false;
    }
}
